package com.ctsi.offlinesupport.app.rest;

import com.ctsi.commons.util.StringUtil;
import com.ctsi.commons.util.UtilDateTime;
import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.common.response.UnifiedResponse;
import com.ctsi.framework.mybatis.support.VoTemplate;
import com.ctsi.framework.organise.security.UserInfo;
import com.ctsi.framework.security.util.SecurityUtil;
import com.ctsi.offlinesupport.dto.CustomtjVO;
import com.ctsi.offlinesupport.dto.ProjecttjVO;
import com.ctsi.offlinesupport.service.CustomEntityService;
import com.ctsi.offlinesupport.service.ProjectEntityService;
import com.ctsi.offlinesupport.service.SupportInfoService;
import com.ctsi.organise.common.api.PersonServiceProxy;
import com.ctsi.organise.common.model.Person;
import com.ctsi.supportflow.app.entity.CustomEntity;
import com.ctsi.supportflow.app.entity.ProjectEntity;
import com.ctsi.supportflow.app.vo.AppCustomVO;
import com.ctsi.supportflow.app.vo.AppProjectVO;
import com.ctsi.supportflow.app.vo.CustomVO;
import com.ctsi.supportflow.app.vo.ProjectVO;
import com.ctsi.system.api.IDictionaryTreeData;
import com.ctsi.system.service.api.DictionaryServiceProxy;
import com.ctsi.system.service.api.DictionayTreeServiceProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目管理
 */
@RestController
public class ProjectController {

    @Autowired
    private ProjectEntityService projectEntityService;
    @Autowired
    private CustomEntityService customEntityService;
    @Autowired
    private PersonServiceProxy personServiceProxy;
    @Autowired
    private VoTemplate voTemplate;
    @Autowired
    private DictionaryServiceProxy dictionaryServiceProxy;
    @Autowired
    private DictionayTreeServiceProxy dictionayTreeServiceProxy;
    @Autowired
    private SupportInfoService supportInfoService;



    /**
     * 添加项目信息
     *
     * @param customId
     * @param projectName
     * @param projectDesc
     * @param isSign
     * @return
     */
    @PostMapping(value = "/project/save", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public UnifiedResponse<String> saveProject(@RequestParam(name = "customId") @NotBlank(message = "客户ID不能为空!") String customId,
                                             @RequestParam(name = "projectName") @NotBlank(message = "项目名称不能为空!") String projectName,
                                             @RequestParam(name = "projectDesc", required = false) String projectDesc,
                                             @RequestParam(name = "isSign", required = false, defaultValue = "0") Integer isSign,
                                             @RequestParam(value = "signTime", required = false) String signTime,
                                             @RequestParam(value = "projectMoney", required = false) Double projectMoney,
                                             @RequestParam(value = "signContractMoney", required = false) Double signContractMoney,
                                             @RequestParam(value = "hangye",required = false) String hangye,
                                               @RequestParam(value = "projectRate",required = false) String projectRate,
                                               @RequestParam(value = "projectType",required = false) String projectType) {
        UserInfo user = SecurityUtil.getUser();
        String userId = user.getUserId();
        /**
         * 校验projectName
         */
        Map<String, Object> params = new HashMap<>(1);
        params.put("projectName", projectName );
        params.put("createBy",userId);
        params.put("customId",customId);
        Page<ProjectEntity> projects = projectEntityService.findByMulitCondition(params, SecurityUtil.getUser().getLoginId(), SecurityUtil.getUser().getUserId(), 0, 15);
        if (projects.getTotalCount() > 0) {
            return UnifiedResponse.error("-1","该项目名称已存在,无需重复添加!", "该项目名称已存在,无需重复添加!");
        }
        ProjectEntity project = new ProjectEntity();
        project.setIsSign(0);
        project.setCustomId(customId);
        project.setProjectDesc(projectDesc);
        project.setProjectName(projectName);
        if (UtilValidate.isNotEmpty(hangye)) {
            project.setHangye(hangye);
            String hyName = dictionaryServiceProxy.getDictValue("hangye_type",hangye);
            project.setHangyeName(hyName);
        }
        if (UtilValidate.isNotEmpty(signTime)){
            project.setSignTime(UtilDateTime.toLong(signTime));
        }
        if (UtilValidate.isNotEmpty(String.valueOf(projectMoney))){
            project.setProjectMoney(projectMoney);
        }
        if (UtilValidate.isNotEmpty(String.valueOf(signContractMoney))){
            project.setSignContractMoney(signContractMoney);
        }
        if (UtilValidate.isNotEmpty(projectType)){
            project.setProjectType(projectType);
            List<String> projectTypeNames = this.dictionayTreeServiceProxy.getTreeDataNameChain("support-type", project.getProjectType());
            project.setProjectTypeName(StringUtil.join(projectTypeNames, "/"));
        }

        Person person = personServiceProxy.findPersonById(userId);
        String dataObjectId = person.getDataObjectId();
        project.setDataObjectId(dataObjectId);
        project.setCreateBy(userId);
        project.setCreateTime(System.currentTimeMillis());
        project.setBeginTime(System.currentTimeMillis());
        project.setProjectStatus(1);
        project.setLastUpdateTime(System.currentTimeMillis());
        projectEntityService.insertProjectEntity(project);
        return UnifiedResponse.ok(project.getId());
    }

    /**
     * 编辑项目信息
     *
     * @return
     */
    @GetMapping(value = "/project/edit/{projectId}")
    public UnifiedResponse<ProjectEntity> editProject(@PathVariable(value = "projectId") String projectId) {
        if (UtilValidate.isEmpty(projectId)){
            return UnifiedResponse.ok(null);
        }
        ProjectEntity projectEntity = projectEntityService.findProjectEntityById(projectId);
        List<IDictionaryTreeData> datas = dictionayTreeServiceProxy.getTreeDataChain("support-type",projectEntity.getProjectType());
        List<String> codes = datas.stream().map(data -> data.getCode()).collect(Collectors.toList());
        ProjectVO vo = new ProjectVO();
        vo.setCodes(codes);
        vo = voTemplate.defaultExcute(vo,projectEntity);
        return UnifiedResponse.ok(vo);
    }

    /**
     * 项目签约接口（app使用）
     */
    @PostMapping(value = "/project/sign")
    public UnifiedResponse<Void> sign(@RequestParam(value = "projectId") String projectId,
                                      @RequestParam(value = "signTime",required = false) String signTime,
                                      @RequestParam(value = "projectMoney",required = false) Double projectMoney,
                                      @RequestParam(value = "signContractMoney",required = false) Double signContractMoney){
        ProjectEntity projectEntity = new ProjectEntity();
        projectEntity.setId(projectId);
        if (UtilValidate.isNotEmpty(signTime)){
            projectEntity.setSignTime(UtilDateTime.toLong(signTime));
        }
        if (UtilValidate.isNotEmpty(String.valueOf(projectMoney))){
            projectEntity.setProjectMoney(projectMoney);
        }
        if (UtilValidate.isNotEmpty(String.valueOf(signContractMoney))){
            projectEntity.setSignContractMoney(signContractMoney);
        }
        projectEntity.setIsSign(1);
        projectEntity.setUpdateBy(SecurityUtil.getUser().getUserId());
        projectEntity.setLastUpdateTime(System.currentTimeMillis());
        int result = projectEntityService.updateProjectEntityByPKSelective(projectEntity);
        if (result > 0) {
            return UnifiedResponse.ok();
        }
        return UnifiedResponse.error("-1","签约操作失败!");
    }

    /**
     * 编解项目
     *
     * @param id
     * @param customId
     * @param projectName
     * @param projectDesc
     * @param isSign
     * @return
     */
    @PutMapping(value = "/project/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public UnifiedResponse<Void> updateProject(@RequestParam(name = "id") @NotBlank(message = "项目ID不能为空!") String id,
                                               @RequestParam(name = "customId") @NotBlank(message = "客户ID不能为空!") String customId,
                                               @RequestParam(name = "projectName", required = false) String projectName,
                                               @RequestParam(name = "projectDesc", required = false) String projectDesc,
                                               @RequestParam(name = "hangye", required = false) String hangye,
                                               @RequestParam(name = "isSign", required = false) Integer isSign,
                                               @RequestParam(value = "signTime", required = false) Long signTime,
                                               @RequestParam(value = "projectMoney", required = false) Double projectMoney,
                                               @RequestParam(value = "signContractMoney", required = false) Double signContractMoney,
                                               @RequestParam(value = "projectType",required = false) String projectType,
                                               @RequestParam(value = "updateTime",required = false) Long updateTime,
                                               @RequestParam(value = "endTime",required = false) Long endTime,
                                               @RequestParam(value = "projectStatus",required = false) Integer projectStatus,
                                                   @RequestParam(value = "projectRate",required = false) Integer projectRate) {
        ProjectEntity project = new ProjectEntity();
        project.setId(id);
        if (UtilValidate.isNotEmpty(hangye)) {
            project.setHangye(hangye);
            String hangyeName = dictionaryServiceProxy.getDictValue("hangye_type",hangye);
            project.setHangyeName(hangyeName);
        }
        if (UtilValidate.isNotEmpty(customId)) {
            project.setCustomId(customId);
        }
        if (UtilValidate.isNotEmpty(projectName)) {
            project.setProjectName(projectName);
        }
        if (UtilValidate.isNotEmpty(String.valueOf(projectRate))){
            project.setProjectRate(projectRate);
        }
        if(UtilValidate.isNotEmpty(String.valueOf(updateTime))){
            project.setLastUpdateTime(updateTime);
        }
        if(UtilValidate.isNotEmpty(String.valueOf(projectStatus))){
            project.setProjectStatus(projectStatus);
        }
        if(UtilValidate.isNotEmpty(String.valueOf(endTime))){
            project.setEndTime(endTime);
        }
        if (UtilValidate.isNotEmpty(projectDesc)) {
            project.setProjectDesc(projectDesc);
        }
        if (UtilValidate.isNotEmpty(projectType)){
            project.setProjectType(projectType);
            List<String> projectTypeNames = this.dictionayTreeServiceProxy.getTreeDataNameChain("support-type", project.getProjectType());
            project.setProjectTypeName(StringUtil.join(projectTypeNames, "/"));
        }
        if (isSign != null) {
            project.setIsSign(isSign);
        }
        if (signTime!=null){
            project.setSignTime(signTime);
        }
        if (UtilValidate.isNotEmpty(String.valueOf(projectMoney))){
            project.setProjectMoney(projectMoney);
        }
        if (UtilValidate.isNotEmpty(String.valueOf(signContractMoney))){
            project.setSignContractMoney(signContractMoney);
        }
        projectEntityService.updateProjectEntityByPKSelective(project);
        return UnifiedResponse.ok();
    }

    /**
     * 删除项目信息
     *
     * @return
     */
    @DeleteMapping(value = "/project/removes")
    public UnifiedResponse<Void> removes(@RequestParam(value = "ids") String ids) {
        String[] _ids = ids.split(",");
        int i = 0;
        for (String id : _ids) {
            if (supportInfoService.findSupportInfoByProjectId(id,0,15).getTotalCount() == 0) {
                /**
                 * 更改为逻辑删除
                 */
                ProjectEntity project = new ProjectEntity();
                project.setId(id);
                project.setIsDel(1);
                //projectEntityService.removeProjectEntity(id);
                projectEntityService.updateProjectEntityByPKSelective(project);
            } else {
                i = ++i;
            }
        }

        if (i > 0) {
            return UnifiedResponse.error("-1","部分项目下存在支撑单，删除失败!");
        }
//        Stream.of(_ids).forEach(id -> {
//            /**
//             * 更改为逻辑删除
//             */
//            ProjectEntity project = new ProjectEntity();
//            project.setId(id);
//            project.setIsDel(1);
//            //projectEntityService.removeProjectEntity(id);
//            projectEntityService.updateProjectEntityByPKSelective(project);
//        });
        return UnifiedResponse.ok();
    }

    /**
     * 检索项目信息
     * @param start
     * @param pageSize
     * @param projectName 项目名称
     * @param customName 客户名称
     * @param customId 客户ID
     * @return
     */
    @PostMapping(value = "/project/query")
    public UnifiedResponse<Page<ProjectVO>> query(@RequestParam(value = "start") int start,
                                                  @RequestParam(value = "pageSize") int pageSize,
                                                  @RequestParam(value = "projectName", required = false) String projectName,
                                                  @RequestParam(value = "customName", required = false) String customName,
                                                  @RequestParam(value = "hangye",required = false) String hangye,
                                                  @RequestParam(value = "customId",required = false) String customId,
                                                  @RequestParam(value = "beginDate" ,required = false) String beginDate,
                                                  @RequestParam(value = "endDate",required = false) String endDate) {
        Map<String, Object> params = new HashMap<>(1);
        if (UtilValidate.isNotEmpty(hangye)) {
            params.put("hangye",hangye);
        }
        if (UtilValidate.isNotEmpty(projectName)) {
            params.put("projectNameLike", "%" + projectName + "%");
        }
        if (UtilValidate.isNotEmpty(customName)) {
            params.put("customName", "%"+customName+"%");
        }
        if (UtilValidate.isNotEmpty(customId)) {
            params.put("customId",customId);
        }
        if (UtilValidate.isNotEmpty(beginDate)) {
            params.put("beginDate", UtilDateTime.toLong(beginDate));
        }
        if (UtilValidate.isNotEmpty(endDate)) {
            params.put("endDate", UtilDateTime.toLong(endDate));
        }
        Page<ProjectEntity> projects = projectEntityService.findByMulitCondition(params, SecurityUtil.getUser().getLoginId(), SecurityUtil.getUser().getUserId(), start, pageSize);
        List<ProjectVO> vos = projects.getResult().stream().map(projectEntity -> {
            ProjectVO vo = voTemplate.defaultExcute(new ProjectVO(), projectEntity);
            vo.setCreateTimeStr(UtilDateTime.longToString(projectEntity.getCreateTime(), UtilDateTime.defaultDatePattern));
            if (projectEntity.getEndTime()!=null){
                vo.setEndTimeStr(UtilDateTime.longToString(projectEntity.getEndTime(),UtilDateTime.defaultDatePattern));
            }
            if (projectEntity.getLastUpdateTime()!=null){
                vo.setUpdateTimeStr(UtilDateTime.longToString(projectEntity.getLastUpdateTime(),UtilDateTime.defaultDatePattern));
            }
            if(projectEntity.getSignTime()!=null){
                vo.setSignTimeStr(UtilDateTime.longToString(projectEntity.getSignTime(),UtilDateTime.defaultDatePattern));
            }
            Person person = personServiceProxy.findPersonById(projectEntity.getCreateBy());
            vo.setCreateByName(person.getFullName());
            return vo;
        }).collect(Collectors.toList());
        return UnifiedResponse.ok(new Page<>(start, projects.getTotalCount(), pageSize, vos));
    }

//    @PostMapping(value = "/project/project-query")
//    public UnifiedResponse<List<Map<String,Object>>> queryProjectByKeyValue(@RequestParam(value = "projectName",required = false) String projectName){
//        Map<String,Object> params = new HashMap<>();
//        if (UtilValidate.isNotEmpty(projectName)) {
//            params.put("projectNameLike",projectName);
//        }
//       List<ProjectEntity> projectEntities = projectEntityService.find(params);
//        List<Map<String,Object>> result = projectEntities.stream().map(projectEntity -> {
//            Map<String,Object> res = new HashMap<>();
//        })
//        return null;
//    }

    /**
     * 客户信息录入
     *
     * @param customName
     * @param customPhone
     * @param customOrganCode
     * @param contacts
     * @param address
     * @return
     */
    @PostMapping(value = "/custom/save", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public UnifiedResponse<Void> customSave(@RequestParam(name = "customName", required = true) String customName,
                                            @RequestParam(name = "customPhone", required = false) String customPhone,
                                            @RequestParam(name = "customOrganCode", required = false) String customOrganCode,
                                            @RequestParam(name = "contacts", required = false) String contacts,
                                            @RequestParam(name = "address", required = false) String address,
                                            @RequestParam(name = "hangye", required = false) String hangye) {
        Map<String,Object> params = new HashMap<>();
        params.put("customName", customName);
        params.put("createBy",SecurityUtil.getUser().getUserId());
        Page<CustomEntity> projects = customEntityService.findByMulitCondition(params, SecurityUtil.getUser().getLoginId(), SecurityUtil.getUser().getUserId(), 0, 15);
        if (projects.getTotalCount() > 0) {
            return UnifiedResponse.error("-1","该客户名称已存在,无需重复添加!");
        }
        CustomEntity customEntity = new CustomEntity();
        customEntity.setCustomName(customName);
        customEntity.setCustomPhone(customPhone);
        customEntity.setCustomOrganCode(customOrganCode);
        customEntity.setContacts(contacts);
        customEntity.setAddress(address);
        customEntity.setHangye(hangye);
        String userId = SecurityUtil.getUser().getUserId();
        Person person = personServiceProxy.findPersonById(userId);
        String dataObjectId = person.getDataObjectId();
        customEntity.setDataObjectId(dataObjectId);
        customEntity.setCreateBy(userId);
        customEntity.setCreateTime(System.currentTimeMillis());
        customEntityService.insertCustomEntity(customEntity);
        return UnifiedResponse.ok();
    }

    /**
     * 客户信息编辑
     *
     * @return
     */
    @GetMapping(value = "/custom/edit/{customId}")
    public UnifiedResponse<CustomEntity> edit(@PathVariable(value = "customId") String customId) {
        CustomEntity customEntity = customEntityService.findCustomEntityById(customId);
        return UnifiedResponse.ok(customEntity);
    }

    /**
     * @param id
     * @param customName
     * @param customPhone
     * @param customOrganCode
     * @param contacts
     * @param address
     * @return
     */
    @PutMapping(value = "/custom/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public UnifiedResponse<Void> update(@RequestParam(name = "id", required = true) String id,
                                        @RequestParam(name = "customName", required = true) String customName,
                                        @RequestParam(name = "customPhone", required = false) String customPhone,
                                        @RequestParam(name = "customOrganCode", required = false) String customOrganCode,
                                        @RequestParam(name = "contacts", required = false) String contacts,
                                        @RequestParam(name = "address", required = false) String address,
                                        @RequestParam(name = "hangye", required = false) String hangye) {
        CustomEntity project = new CustomEntity();
        project.setId(id);
        if (UtilValidate.isNotEmpty(customName)) {
            project.setCustomName(customName);
        }
        if (UtilValidate.isNotEmpty(hangye)) {
            project.setHangye(hangye);
        }
        if (UtilValidate.isNotEmpty(customPhone)) {
            project.setCustomPhone(customPhone);
        }
        if (UtilValidate.isNotEmpty(customOrganCode)) {
            project.setCustomOrganCode(customOrganCode);
        }
        if (UtilValidate.isNotEmpty(contacts)) {
            project.setContacts(contacts);
        }
        if (UtilValidate.isNotEmpty(address)) {
            project.setAddress(address);
        }
        customEntityService.updateCustomEntityByPKSelective(project);
        return UnifiedResponse.ok();
    }

    /**
     * 更新客户，post请求
     * @param id
     * @param customName
     * @param customPhone
     * @param customOrganCode
     * @param contacts
     * @param address
     * @param hangye
     * @return
     */
    @PostMapping(value = "/custom/updateapi", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public UnifiedResponse<Void> updateCustom(@RequestParam(name = "id", required = true) String id,
                                        @RequestParam(name = "customName", required = false) String customName,
                                        @RequestParam(name = "customPhone", required = false) String customPhone,
                                        @RequestParam(name = "customOrganCode", required = false) String customOrganCode,
                                        @RequestParam(name = "contacts", required = false) String contacts,
                                        @RequestParam(name = "address", required = false) String address,
                                        @RequestParam(name = "hangye", required = false) String hangye) {
        CustomEntity project = new CustomEntity();
        project.setId(id);
        if (UtilValidate.isNotEmpty(customName)) {
            project.setCustomName(customName);
        }
        if (UtilValidate.isNotEmpty(hangye)) {
            project.setHangye(hangye);
        }
        if (UtilValidate.isNotEmpty(customPhone)) {
            project.setCustomPhone(customPhone);
        }
        if (UtilValidate.isNotEmpty(customOrganCode)) {
            project.setCustomOrganCode(customOrganCode);
        }
        if (UtilValidate.isNotEmpty(contacts)) {
            project.setContacts(contacts);
        }
        if (UtilValidate.isNotEmpty(address)) {
            project.setAddress(address);
        }
        customEntityService.updateCustomEntityByPKSelective(project);
        return UnifiedResponse.ok();
    }
    /**
     * 客户信息删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/custom/removes")
    public UnifiedResponse<Void> customRemoves(@RequestParam(value = "ids") String ids) {
        String[] _ids = ids.split(",");
        int i = 0;
        for (String id : _ids) {
            if(supportInfoService.findSupportInfoByCustomId(id,0,15).getTotalCount() == 0) {
                customEntityService.removeCustom(id);
            } else {
                i=++i;
            }
        }
        if (i>0) {
            return UnifiedResponse.error("-1","部分客户下存在支撑单,删除失败!");
        }
        return UnifiedResponse.ok();
    }
    /**
     * 驾驶舱 客户统计
     */
    @PostMapping(value = "/custom/leaderCockpitCustomTj")
    public UnifiedResponse<CustomtjVO> selectLeaderCockpitCustom() {
        Map<String, Object> map = customEntityService.selectLeaderCockpitCustom();
        CustomtjVO customtjVO=new CustomtjVO();
        customtjVO.setSumnum(Long.parseLong( map.get("sumnum").toString()));
        customtjVO.setMonthnum(Long.parseLong( map.get("monthnum").toString()));
        customtjVO.setYearnum(Long.parseLong(map.get("yearnum").toString()));
        customtjVO.setLastmonthnum(Long.parseLong(map.get("lastmonthnum").toString()));
        customtjVO.setLastyearnum(Long.parseLong(map.get("lastyearnum").toString()));
        return UnifiedResponse.ok(customtjVO);
    }

    /**
     * 驾驶舱 项目统计
     */
    @PostMapping(value = "/project/selectLeaderProjectTj")
    public UnifiedResponse<ProjecttjVO> selectLeaderProjectTj() {
        Map<String, Object> map = projectEntityService.selectLeaderProjectTj();
        ProjecttjVO projecttjVO=new ProjecttjVO();
        projecttjVO.setSqsumnum(Long.parseLong( map.get("sqsumnum").toString()));
        projecttjVO.setSqmonthnum(Long.parseLong( map.get("sqmonthnum").toString()));
        projecttjVO.setSqyearnum(Long.parseLong(map.get("sqyearnum").toString()));
        projecttjVO.setSqlastmonthnum(Long.parseLong(map.get("sqlastmonthnum").toString()));
        projecttjVO.setSqlastyearnum(Long.parseLong(map.get("sqlastyearnum").toString()));

        projecttjVO.setSzsumnum(Long.parseLong( map.get("szsumnum").toString()));
        projecttjVO.setSzmonthnum(Long.parseLong( map.get("szmonthnum").toString()));
        projecttjVO.setSzyearnum(Long.parseLong(map.get("szyearnum").toString()));
        projecttjVO.setSzlastmonthnum(Long.parseLong(map.get("szlastmonthnum").toString()));
        projecttjVO.setSzlastyearnum(Long.parseLong(map.get("szlastyearnum").toString()));

        projecttjVO.setShsumnum(Long.parseLong( map.get("shsumnum").toString()));
        projecttjVO.setShmonthnum(Long.parseLong( map.get("shmonthnum").toString()));
        projecttjVO.setShyearnum(Long.parseLong(map.get("shyearnum").toString()));
        projecttjVO.setShlastmonthnum(Long.parseLong(map.get("shlastmonthnum").toString()));
        projecttjVO.setShlastyearnum(Long.parseLong(map.get("shlastyearnum").toString()));
        return UnifiedResponse.ok(projecttjVO);
    }

    /**
     * 客户信息查询
     *
     * @param start
     * @param pageSize
     * @param customName
     * @return
     */
    @PostMapping(value = "/custom/query")
    public UnifiedResponse<Page<CustomVO>> customQuery(@RequestParam(value = "start") int start,
                                                       @RequestParam(value = "pageSize") int pageSize,
                                                       @RequestParam(value = "partyId" ,required = false) String partyId,
                                                       @RequestParam(value = "customName", required = false) String customName,
                                                       @RequestParam(value = "beginDate" ,required = false) String beginDate,
                                                       @RequestParam(value = "endDate",required = false) String endDate) {
        Map<String, Object> params = new HashMap<>(1);

        if (UtilValidate.isNotEmpty(customName)) {
            params.put("customNameLike", "%" + customName + "%");
        }
        if (UtilValidate.isNotEmpty(partyId)) {
            params.put("partyId",partyId);
        }
        if (UtilValidate.isNotEmpty(beginDate)) {
            params.put("beginDate", UtilDateTime.toLong(beginDate));
        }
        if (UtilValidate.isNotEmpty(endDate)) {
            params.put("endDate", UtilDateTime.toLong(endDate));
        }
        Page<CustomEntity> projects = customEntityService.findByMulitCondition(params, SecurityUtil.getUser().getLoginId(), SecurityUtil.getUser().getUserId(), start, pageSize);

        List<CustomVO> vos = projects.getResult().stream().map(projectEntity -> {
            String hangyecode = projectEntity.getHangye();
            String dictValue = dictionaryServiceProxy.getDictValue("hangye_type", hangyecode);
            CustomVO vo = voTemplate.defaultExcute(new CustomVO(), projectEntity);
            vo.setCreateTimeStr(UtilDateTime.longToString(projectEntity.getCreateTime(), UtilDateTime.defaultDatePattern));
            vo.setHangyename(dictValue);
            Person person = personServiceProxy.findPersonById(projectEntity.getCreateBy());
            vo.setCreateByName(person.getFullName());
            return vo;
        }).collect(Collectors.toList());
        return UnifiedResponse.ok(new Page<>(start, projects.getTotalCount(), pageSize, vos));
    }

    @PostMapping(value = "/custom/fetch")
    public UnifiedResponse<List<Map<String, Object>>> fetchCustom(@RequestParam(value = "customName", required = false) String customName) {
        Map<String, Object> params = new HashMap<>();
        if (UtilValidate.isNotEmpty(customName)) {
            params.put("customName", "%" + customName + "%");
        }
        params.put("createBy", SecurityUtil.getUser().getUserId());
        List<CustomEntity> customEntities = customEntityService.find(params);
        List<Map<String, Object>> result = customEntities.stream().map(custom -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", custom.getId());
            map.put("label", custom.getCustomName());
            return map;
        }).collect(Collectors.toList());
        return UnifiedResponse.ok(result);
    }

    /**
     * 检索项目
     *
     * @param projectName
     * @param customId
     * @return
     */
    @PostMapping(value = "/project/fetch")
    public UnifiedResponse<List<Map<String, Object>>> fetchProject(@RequestParam(value = "projectName", required = false) String projectName,
                                                                   @RequestParam(value = "customId", required = false) String customId) {
        Map<String, Object> params = new HashMap<>();
        if (UtilValidate.isNotEmpty(projectName)) {
            params.put("projectName", "%" + projectName + "%");
        }

        if (UtilValidate.isNotEmpty(customId)) {
            params.put("customId", customId);
        }

        params.put("createBy", SecurityUtil.getUser().getUserId());
        List<ProjectEntity> projectEntities = projectEntityService.find(params);

        List<Map<String, Object>> result = projectEntities.stream().map(custom -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", custom.getId());
            map.put("label", custom.getProjectName());
            return map;
        }).collect(Collectors.toList());

        return UnifiedResponse.ok(result);
    }

    /**
     * app 项目接口
     * @return
     */
    @PostMapping(value = "/project/hynum")
    public UnifiedResponse<AppProjectVO> queryProjectHy(@RequestParam(value = "beginDate" ,required = false) String beginDate,
                                                        @RequestParam(value = "endDate",required = false) String endDate){
        String userId = SecurityUtil.getUser().getLoginId();
        Map<String,Object> params = new HashMap<>();
        if (UtilValidate.isNotEmpty(beginDate)) {
            params.put("beginDate", UtilDateTime.toLong(beginDate));
        }
        if (UtilValidate.isNotEmpty(endDate)) {
            params.put("endDate", UtilDateTime.toLong(endDate));
        }
        return UnifiedResponse.ok(projectEntityService.findProjectHyVO(params,userId));
    }

    @PostMapping(value = "/project/customnum")
    public UnifiedResponse<AppCustomVO> queryAppCustom(@RequestParam(value = "ancestorId",required = false,defaultValue = "root") String ancestorId,
                                                       @RequestParam(value = "beginDate" ,required = false) String beginDate,
                                                       @RequestParam(value = "endDate",required = false) String endDate){
        String userId = SecurityUtil.getUser().getLoginId();
        Map<String,Object> params = new HashMap<>();
        if (UtilValidate.isNotEmpty(beginDate)) {
            params.put("beginDate", UtilDateTime.toLong(beginDate));
        }
        if (UtilValidate.isNotEmpty(endDate)) {
            params.put("endDate", UtilDateTime.toLong(endDate));
        }
        params.put("ancestorId",ancestorId);
        return UnifiedResponse.ok(projectEntityService.findAppCustom(params,userId));
    }


}
